﻿@page "/SysClient/Details/{id}"
@using SelfService.ViewModel.SysClientVMs;
@using SelfService.Model.Models
@using SelfService.ViewModel.SysClientTaskVMs
@inherits BasePage

<ValidateForm @ref="vform" Model="@Model" >
    <Row ItemsPerRow="ItemsPerRow.Two" RowType="RowType.Normal">

        <Display @bind-Value="@Model.Entity.Name"   ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.Code"   ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.Address"   ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.Ip"   ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.MacAddress"   ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.Version"   ShowLabel="true"/>
        <Display @bind-Value="@Model.ThemeName"   ShowLabel="true"/>
        <Display @bind-Value="@Model.SelectedModulesIDs" Lookup="@AllSysModules"  ShowLabel="true"/>
        <Display @bind-Value="@Model.SelectedDevicesIDs" Lookup="@AllSysDevices"  ShowLabel="true"/>
        <Display @bind-Value="@Model.SelectedPayTypesIds" Lookup="@AllSysPayTypes"  ShowLabel="true"/>
        <Display @bind-Value="@Model.SelectedCardTypesIds" Lookup="@AllSysCardTypes"  ShowLabel="true"/>
        <Switch @bind-Value="@Model.Entity.IsUpdate"  IsDisabled="true" ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.LastTime"   ShowLabel="true"/>
        <Display @bind-Value="@Model.Entity.LoginTime"   ShowLabel="true"/>
    </Row>
    <Row ItemsPerRow="ItemsPerRow.Four" RowType="RowType.Normal" style="margin: 20px 0;">
        <PopConfirmButton 
            Icon="fa fa-refresh" 
            Text="强制刷新" 
            OnConfirm="() => OnClientActionClick(Model, ClientTaskActionEnum.强制刷新)" 
            Color="Color.Primary" 
            Size="Size.ExtraSmall"
            Content="确定强制刷新吗?" 
            CloseButtonText="取消" 
            ConfirmButtonText="确认" 
            ConfirmButtonColor="Color.Primary"/>
        <PopConfirmButton 
            Icon="fa fa-refresh" 
            Text="强制重启" 
            OnConfirm="() => OnClientActionClick(Model, ClientTaskActionEnum.强制重启)" 
            Color="Color.Warning" 
            Size="Size.ExtraSmall"
            Content="确定强制重启吗?" 
            CloseButtonText="取消" 
            ConfirmButtonText="确认" 
            ConfirmButtonColor="Color.Warning"/>
        <PopConfirmButton 
            Icon="fa fa-window-close-o" 
            Text="强制关闭客户端" 
            OnConfirm="() => OnClientActionClick(Model, ClientTaskActionEnum.强制关闭)" 
            Color="Color.Danger" 
            Size="Size.ExtraSmall"
            Content="确定强制关闭客户端吗?" 
            CloseButtonText="取消" 
            ConfirmButtonText="确认" 
            ConfirmButtonColor="Color.Danger"/>
        <PopConfirmButton 
            Icon="fa fa-window-close-o" 
            Text="关机" 
            OnConfirm="() => OnClientActionClick(Model, ClientTaskActionEnum.强制关机)" 
            Color="Color.Danger" 
            Size="Size.ExtraSmall"
            Content="确定强制关机吗?" 
            CloseButtonText="取消" 
            ConfirmButtonText="确认" 
            ConfirmButtonColor="Color.Danger"/>
    </Row>
    <div class="modal-footer table-modal-footer">
        <Button Color="Color.Primary" Icon="fa fa-save" Text="@WtmBlazor.Localizer["Sys.Close"]" OnClick="OnClose" />
    </div>
</ValidateForm>

@code {

    private SysClientVm Model = null;
    private ValidateForm vform { get; set; }
    [Parameter]
    public string id { get; set; }

    private List<SelectedItem> AllSysModules = new List<SelectedItem>();

    private List<SelectedItem> AllSysDevices = new List<SelectedItem>();
    private List<SelectedItem> AllSysCardTypes = new List<SelectedItem>();

    private List<SelectedItem> AllSysPayTypes = new List<SelectedItem>();

    protected override async Task OnInitializedAsync()
    {

        AllSysModules = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysModules", placeholder: WtmBlazor.Localizer["Sys.All"]);

        AllSysDevices = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysDevices", placeholder: WtmBlazor.Localizer["Sys.All"]);

        AllSysCardTypes = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysCardTypes", placeholder: WtmBlazor.Localizer["Sys.All"]);

        AllSysPayTypes = await WtmBlazor.Api.CallItemsApi("/api/SysClient/GetSysPayTypes", placeholder: WtmBlazor.Localizer["Sys.All"]);

        var rv = await WtmBlazor.Api.CallAPI<SysClientVm>($"/api/SysClient/{id}");
        Model = rv.Data;
    }

    private async Task OnClientActionClick(SysClientVm item, ClientTaskActionEnum action)
    {
        var vm = new SysClientTaskVm();
        vm.Entity.ClientId = item.Entity.ID;
        vm.Entity.Action = action;
        await PostsData(vm, $"/api/SysClientTask/add", (s) => "Sys.OprationSuccess");
    }

    public void OnClose()
    {
        CloseDialog();
    }

}
